/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2025-05-22 21:43:50
 * @LastEditors: sueRimn
 * @LastEditTime: 2025-08-06 20:37:53
 */

#ifndef _POWER_H
#define _POWER_H

#include "main.h"
#include "stdbool.h"

#define POUT_24V_CT1_ON     {HAL_GPIO_WritePin(POUT_24V_CT1_GPIO_Port,POUT_24V_CT1_Pin,GPIO_PIN_SET);pstDev->stDevPowerSupply.bPower24VPort1 = true;}         //4p
#define POUT_24V_CT1_OFF    {HAL_GPIO_WritePin(POUT_24V_CT1_GPIO_Port,POUT_24V_CT1_Pin,GPIO_PIN_RESET);pstDev->stDevPowerSupply.bPower24VPort1 = false;}

#define POUT_24V_CT2_ON     {HAL_GPIO_WritePin(POUT_24V_CT2_GPIO_Port,POUT_24V_CT2_Pin,GPIO_PIN_SET);pstDev->stDevPowerSupply.bPower24VPort2 = true;}         //2p
#define POUT_24V_CT2_OFF    {HAL_GPIO_WritePin(POUT_24V_CT2_GPIO_Port,POUT_24V_CT2_Pin,GPIO_PIN_RESET);pstDev->stDevPowerSupply.bPower24VPort2 = false;}

#define POUT_12V_CT1_ON     {HAL_GPIO_WritePin(POUT_12V_CT1_GPIO_Port,POUT_12V_CT1_Pin,GPIO_PIN_SET);pstDev->stDevPowerSupply.bPower12VPort1 = true;}         //2p
#define POUT_12V_CT1_OFF    {HAL_GPIO_WritePin(POUT_12V_CT1_GPIO_Port,POUT_12V_CT1_Pin,GPIO_PIN_RESET);pstDev->stDevPowerSupply.bPower12VPort1 = false;}

#define POUT_12V_CT2_ON     {HAL_GPIO_WritePin(POUT_12V_CT2_GPIO_Port,POUT_12V_CT2_Pin,GPIO_PIN_SET);pstDev->stDevPowerSupply.bPower12VPort2 = true;}         //4p
#define POUT_12V_CT2_OFF    {HAL_GPIO_WritePin(POUT_12V_CT2_GPIO_Port,POUT_12V_CT2_Pin,GPIO_PIN_RESET);pstDev->stDevPowerSupply.bPower12VPort2 = false;}

#define POUT_5V_CT_ON       {HAL_GPIO_WritePin(POUT_5V_CT_GPIO_Port,POUT_5V_CT_Pin,GPIO_PIN_RESET);pstDev->stDevPowerSupply.bPower5VPort = true;}           
#define POUT_5V_CT_OFF      {HAL_GPIO_WritePin(POUT_5V_CT_GPIO_Port,POUT_5V_CT_Pin,GPIO_PIN_SET);pstDev->stDevPowerSupply.bPower5VPort = false;}

#define PDEV_ICM_ON         HAL_GPIO_WritePin(OUT_CS_GPIO_Port, OUT_CS_Pin, GPIO_PIN_RESET) //和陀螺仪spi_cd引脚在一起 在片选时会自动打开陀螺仪供电
#define PDEV_ICM_OFF        HAL_GPIO_WritePin(OUT_CS_GPIO_Port, OUT_CS_Pin, GPIO_PIN_SET)

#define PDEV_5V_ON          {HAL_GPIO_WritePin(DEV_5V_C_GPIO_Port, DEV_5V_C_Pin, GPIO_PIN_SET);  pstDev->stDevPowerSupply.bPowerDev5VPort = true;}
#define PDEV_5V_OFF         {HAL_GPIO_WritePin(DEV_5V_C_GPIO_Port, DEV_5V_C_Pin, GPIO_PIN_RESET);  pstDev->stDevPowerSupply.bPowerDev5VPort = false;}

#define PCHARG_OFF          HAL_GPIO_WritePin(CHARG_C_GPIO_Port,CHARG_C_Pin,GPIO_PIN_SET)
#define PCHARG_ON           HAL_GPIO_WritePin(CHARG_C_GPIO_Port,CHARG_C_Pin,GPIO_PIN_RESET)

#define READ_PSWITCH        (HAL_GPIO_ReadPin(POWER_B_GPIO_Port,POWER_B_Pin))

#define BAT_AUTO_STOP_CHARG_VOL 24900

//A1=GND,A2=GND // R=1, W=0 //0x80
#define INA_ADDR_0                 0x80	//0x81

//A1=VCC,A2=VCC // R=1, W=0 //0x8A
#define INA_ADDR_3                 0x8A		//0x80

#define Config_Reg                 0x00
#define Shunt_V_Reg                0x01		//分流电压， 此处分流电阻为 0.1欧
#define Bus_V_Reg                  0x02		//总线电压
#define Power_Reg                  0x03		//电源功率
#define Current_Reg                0x04		//电流
#define Calib_Reg                  0x05		//校准，设定满量程范围以及电流和功率测数的 
#define Mask_En_Reg                0x06		//屏蔽 使能 警报配置和转换准备就绪
#define Alert_Reg                  0x07		//包含与所选警报功能相比较的限定值
#define Man_ID_Reg                 0xFE  	//0x5449
#define ID_Reg                     0xFF  	//包含唯一的芯片标识号



typedef struct _power_supply
{
    bool bPower24VPort1;
    bool bPower24VPort2;
    bool bPower12VPort1;
    bool bPower12VPort2;
    bool bPower5VPort;
    bool bPowerDev5VPort;

}power_supply_t;

int8_t power_init(void);
int8_t INA_init(void);
// void INA_Read(uint8_t ucInaChoose);
void powerCtrl(uint8_t *pucPowerCtrl);
void powerRunTask(void);

void powerOn(void);
void powerOff(void);
int16_t calculate_soc_int16();
void chargCheck(void);
#endif
